﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections.Generic;

namespace SqlconTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //连接字符串
        public static string constr = "Server=.;user Id=sa;pwd=123456;database=Test;";
        //加载驱动
        public static SqlConnection sqlcon = new SqlConnection(constr);
        //加载程序的时候判断数据库是否关闭并自动打开数据库
        private void Form1_Load(object sender, EventArgs e)
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
        }
        //增加
        private void btn_Insert_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
            form2.BringToFront();//窗体显示在最前端
        }
        //删除
        private void btn_Del_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(this.txt_Id.Text);
            string name = this.txt_Name.Text;
            string sql = "delete from stuInfo where Id=" + id + " and Name='" + name + "'";
            SqlCommand command = new SqlCommand(sql, sqlcon);
            int result = command.ExecuteNonQuery();
            if (result >= 1)
            {
                MessageBox.Show("删除成功", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("删除失败", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //修改
        private void btn_Update_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(this.txt_Id.Text);
            string name = this.txt_Name.Text;
            string sql = "update StuInfo set Name='"+name+"' where Id="+id+"";
            SqlCommand command = new SqlCommand(sql,sqlcon);
            int result = command.ExecuteNonQuery();
            if (result>=1)
            {
                MessageBox.Show("修改成功","消息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("修改失败","消息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        //查询
        private void btn_Select_Click(object sender, EventArgs e)
        {
            string id = this.txt_Id.Text;
            if (id == "")
            {
                var dt = DbHelper.GetTable("select * from StuInfo");
                this.dataGridView1.DataSource = dt;
            }
            else
            {
                var dt = DbHelper.GetTable("select * from StuInfo where Id="+id+"");
                this.dataGridView1.DataSource = dt;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //单击更换背景色
            List<Color> colors = new List<Color>();
            colors.Add(Color.Red);
            colors.Add(Color.Black);
            colors.Add(Color.Blue);
            colors.Add(Color.White);
            colors.Add(Color.Yellow);
            Random random = new Random();
            var randInt = random.Next(0,colors.Count);
            var randColor = colors[randInt];
            BackColor = randColor;
        }
    }
}

